package com.test.socket.controller;

import com.test.socket.message.SimpleMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;

/**
 * @author zxs
 * @since 2021/6/17
 */
@Controller
public class HelloController {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;


    @MessageMapping("/im")
    public void hello(SimpleMessage message) {

        this.messagingTemplate.convertAndSendToUser(message.getTo(), "/queue/messages", message);
        this.messagingTemplate.convertAndSendToUser(message.getFrom(), "/queue/messages", message);
    }

    @SubscribeMapping("/subscribe")
    public String handleSubscribe() {

        return "subscribes";
    }
}
